﻿
var l_profileId = 0;

/****************************************************
funciton: createProfile
parameters: destination, firstname, lastname, email
*****************************************************/
function createProfile() {
    $.ajax({
        url: 'Class/insertProfile.aspx',
        data: { destination: $("#txtDestination")[0].value, name: $("#txtName")[0].value, email: $("#txtEmail")[0].value },
        success: function (data) {
            // set the profile id to a local, which used by other functions within the class
            l_profileId = data.result;

            //*** when result is great than 0 which means profile has been created successful

            // getIPLocation then create entry of where user coming from
            getIPLocation();

        },
        dataType: "json",
        error: function (xhr, text, err) {
            alert(text)
        }
    });
}

/****************************************************
funciton: getIPLocation
*****************************************************/
function getIPLocation() {
    EasyjQuery_Get_IP("my_callbackAddress", "full"); // full version
}

/****************************************************
funciton: my_callbackAddress
*****************************************************/
function my_callbackAddress(json) {
    // more information at http://api.easyjquery.com/test/demo-ip.php
    //alert("IP :" + json.IP + " \nCOUNTRY: " + json.COUNTRY + " City: " + json.cityName + " regionName: " + json.regionName);

//string ip, string countryCode, string countryName, string regionName, string city, string latitude, string longitude, string continentName, Int32 profileId
    $.ajax({
        url: 'Class/insertProfileLocationInfo.aspx',
        data: { ip: json.IP,
                countryCode: json.countryCode3,
                countryName: json.countryName,
                regionName: json.regionName,
                city: json.cityName,
                latitude: json.cityLatitude,
                longitude: json.cityLongitude,
                continentName: json.continentName,
                profileId: l_profileId
            },
            success: function (data) {

            //*** when result is true which means the profilelocation has created successfully.
            //alert(data.result);
        },
        dataType: "json",
        error: function (xhr, text, err) {
            alert(text)
        }
    });
}